package com.frxuexi.park.adapter

import android.widget.TextView
import androidx.databinding.DataBindingUtil
import com.blankj.utilcode.util.TimeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.frxuexi.lib_base.room.ParkEntity
import com.frxuexi.lib_base.utils.DateTimeUtils
import com.frxuexi.park.R
import com.frxuexi.park.databinding.ItemParkpreBinding

class ParkPreAdapter : BaseQuickAdapter<ParkEntity, BaseViewHolder>(R.layout.item_parkpre) {

    override fun onItemViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
        super.onItemViewHolderCreated(viewHolder, viewType)
        DataBindingUtil.bind<ItemParkpreBinding>(viewHolder.itemView)
    }

    override fun convert(helper: BaseViewHolder, item: ParkEntity) {
        val binding = DataBindingUtil.getBinding<ItemParkpreBinding>(helper.itemView)!!
        binding.bean = item

        binding.parkpreTime.apply {
            if (item.time == "上午") {
                setText("${item.date} 9:00:00-12:00:00")
            } else if (item.time == "下午") {
                setText("${item.date} 14:00:00-16:00:00")
            } else {
                setText("${item.date} 18:00:00-22:00:00")
            }
        }

        if (item.time == "下午") {
            getStatus(binding.parkpreStatus, "14:00:00", "16:00:00")
        } else if (item.time == "上午") {
            getStatus(binding.parkpreStatus, "09:00:00", "12:00:00")
        } else {
            getStatus(binding.parkpreStatus, "18:00:00", "22:00:00")
        }
    }

    fun getStatus(tv: TextView, start: String, end: String) {
        val currentTime = DateTimeUtils.getTime(System.currentTimeMillis())
        tv.apply {
            if (currentTime > end) {
                setText("已使用")
                setTextColor(context.getColor(com.frxuexi.lib_base.R.color.darkorange))
                setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_weihu)
            } else if (currentTime < start) {
                setText("待使用")
                setTextColor(context.getColor(com.frxuexi.lib_base.R.color.green))
                setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_status)
            } else {
                setText("使用中")
                setTextColor(context.getColor(com.frxuexi.lib_base.R.color.blue))
                setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_zhanyong)
            }
        }
    }
}